package logic

import (
	"context"
	"errors"
	"weiyun-management-system/models/mysql"

	"weiyun-management-system/weirpc/internal/svc"
	"weiyun-management-system/weirpc/weirpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserPerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserPerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserPerfectLogic {
	return &UserPerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserPerfectLogic) UserPerfect(in *weirpc.UserPerfectRequest) (*weirpc.UserPerfectResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	if err := user.FindUseerbyId(mysql.DB, in.UserId); err != nil {
		return nil, errors.New("用户不存在")
	}
	user.NickName = in.RealName
	user.HeadImage = in.HeadImage
	user.Address = in.Address
	user.Age = in.Age
	user.Email = in.Email

	err := user.UpdateUser(mysql.DB)
	if err != nil {
		return nil, errors.New("完善信息失败")
	}

	return &weirpc.UserPerfectResponse{
		UserId: int64(user.ID),
	}, nil
}
